//
//  UIButton+Addition.m
//  CLAVFoundationMedia
//
//  Created by Criss on 2017/2/7.
//  Copyright © 2017年 Criss. All rights reserved.
//

#import "UIButton+Addition.h"
#import <objc/runtime.h>

static char* btnActionKey = "btnActionKey";

@interface UIButton ()

@property (nonatomic, copy) addActionBlock addAction;

@end

@implementation UIButton (Addition)

+ (instancetype)buttonWithNormalTitle:(NSString *)title titleColor:(UIColor *)color superView:(UIView *)superView addAction:(addActionBlock)addAction{
    UIButton *btn = [self buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    if (color) {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    [btn sizeToFit];
    [superView addSubview:btn];
    if (addAction) {
        btn.addAction = addAction;
        [btn addTarget:btn action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

- (void)setAddAction:(addActionBlock)addAction {
    objc_setAssociatedObject(self, btnActionKey, addAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (addActionBlock)addAction {
    return objc_getAssociatedObject(self, btnActionKey);
}

- (void)btnAction:(UIButton *)sender {
    if (self.addAction) {
        self.addAction(sender);
    }
}

- (void)addClickAction:(addActionBlock)addAction {
    self.addAction = addAction;
    [self addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
}

@end
